package com.wocao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wocao.entity.ResponseResult;
import com.wocao.entity.SysPost;
import com.wocao.service.SysPostService;
import com.wocao.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 岗位信息表 前端控制器
 */
@RestController
@RequestMapping("/sysPost")
public class SysPostController {

    @Autowired
    private SysPostService sysPostService;

    //查所有可用岗位
    @GetMapping("posts")
    public ResponseResult all(){
        List<SysPost> list = sysPostService.list(new QueryWrapper<SysPost>().eq("status","0"));
        return ResponseResult.ok(list);
    }

    @GetMapping("/findPage")
    public ResponseResult getSysPostPage(@RequestParam(required = false, defaultValue = "1") Long current,
                                         @RequestParam(required = false, defaultValue = "5") Long size,
                                         @RequestParam(required = false, defaultValue = "") String code,
                                         @RequestParam(required = false, defaultValue = "") String name,
                                         @RequestParam(required = false, defaultValue = "0") String status) {
        try {
            IPage<SysPost> sysPostIPage = sysPostService.getSysPostPage(current, size, code, name, status);
            return ResponseResult.ok(sysPostIPage);
        } catch (Exception e) {
            return ResponseResult.error(500L, "查询失败" + e);
        }
    }


    /**
     * 新增岗位管理
     *
     * @param sysPost
     * @return
     */
    @PostMapping("/addSysPost")
    public ResponseResult addSysPost(@RequestBody SysPost sysPost) {

        try {
            sysPostService.addSysPost(sysPost);
            return ResponseResult.ok("添加成功");
        } catch (Exception e) {
            return ResponseResult.error(500L, "添加失败" + e);
        }
    }


    /**
     * 修改岗位管理
     *
     * @param sysPost
     * @return
     */
    @PutMapping("/updateSysPost")
    public ResponseResult updateSysPost(@RequestBody SysPost sysPost) {
        try {
            sysPostService.updateById(sysPost);
            return ResponseResult.ok("修改成功");
        } catch (Exception e) {
            return ResponseResult.error(500L, "修改失败" + e);
        }
    }


    @DeleteMapping("/delSysPost/{ids}")
    public ResponseResult delSysPost(@PathVariable List<Long> ids) {
        try {
            sysPostService.deleteIds(ids);
            return ResponseResult.ok("删除成功");
        } catch (Exception e) {
            return ResponseResult.error(500L, "删除失败" + e);
        }
    }

    /**
     * SysPost导出
     *
     * @param resp
     */
    @GetMapping("/exportSysPost")
    public void exportSysPost(HttpServletResponse resp) {
        List<SysPost> list = sysPostService.list();
        try {
            ExcelUtil.export(resp, SysPost.class, list);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

}
